<?php

/**
 * +----------------------------------------------------------------------
 * | TickyPHP [ This is a freeware ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2019 All rights reserved.
 * +----------------------------------------------------------------------
 * | Author: 罗敏贵 <e-mail:minguiluo@163.com> <QQ:271391233>
 * +----------------------------------------------------------------------
 * | SVN: $Id: Tag.php 78891 2019-01-28 14:48:08 luomingui $
 * +----------------------------------------------------------------------
 * | 文件功能：对应的表名:tky_tag
 * +----------------------------------------------------------------------
 */

namespace application\admin\controller;

use ticky\request;

class tag extends base {

    // 首页
    public function index() {
        $ret = db('tag')->order('id desc')->newpage();
        $this->assign('page', $ret['page']);
        $this->assign('items', $ret['items']);
        $this->assign('search', $search['arr']);
        $this->display('tag/index');
    }

    // 添加记录
    public function add() {
        if (request::isPost()) {
            $_POST['tags'] = str_replace('，', ',', strip_tags($_POST['tags']));
            $arr = array_unique(explode(',', $_POST['tags']));
            foreach ($arr as $val) {
                $tagid = db('tag')->where(array('tag' => $val))->find();
                if (!$tagid) {
                    db('tag')->insert(array('tag' => $val, 'total' => 0, 'inputtime' => TIMESTAMP));
                }
            }
            showmsg('添加成功', '/admin/tag');
        } else {
            $this->assign('postUrl', '/admin/tag/add');
            $this->assign('action', '添加');
            $this->display('tag/manage');
        }
    }

    // 更新记录
    public function update() {
        if (request::isPost()) {
            $data = $this->post_frm();
            $id = request::post('id', 0);
            db('tag')->where(['id' => $id])->update($data);

            showmsg('修改成功', '/admin/tag');
        } else {
            $id = request::get('id', 0);
            $item = db('tag')->where(['id' => $id])->find();
            $this->assign('postUrl', '/admin/tag/update');
            $this->assign('action', '修改');
            $this->assign('item', $item);
            $this->display('tag/manage');
        }
    }

    // 删除记录
    public function delete() {
        $id = request::get('id', 0);
        $result = db('tag')->delete(['id' => $id]);
        showmsg('删除成功', '/admin/tag');
    }

    // 批量删除
    public function batchremove() {
        $optype = request::post('optype', 'del');
        $ids = request::post('ids', []);
        if ($optype == "del") {
            for ($i = 0; $i < count($ids); $i++) {
                $id = $ids[$i];
                db('tag')->delete(['id' => $id]);
            }
            showmsg('删除成功', '/admin/tag');
        }
    }

    //表单数据
    private function post_frm() {
        $arr = array();
        $arr['tag'] = request::post('tags', '');
        $arr['total'] = request::post('total', '0');
        $arr['inputtime'] = TIMESTAMP;
        return $arr;
    }

}
